package com.hangup.activity;

import com.devil.bbsclient.R;
import com.hangup.util.PublicUtil;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MoreActivity extends BBSActivity {

	private TextView tv;
	private View moreView;

	private Button userBt;
	private Button exitBt;
	private PublicApplication pa;
	private TextView userTV;
	private Button rbt;
	private Button cityBt;
	private EditText cityCt;

	Handler handler_ = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.what == 1)
				showInfo();
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		tv = (TextView) findViewById(R.id.titleText);
		tv.setText(R.string.tabhost_tab4);

		RelativeLayout rl = (RelativeLayout) findViewById(R.id.view_main);
		LayoutInflater inflater = getLayoutInflater();
		moreView = inflater.inflate(R.layout.activity_more, null);
		rl.addView(moreView);

		pa = (PublicApplication) getApplicationContext();
		if (pa.isLogin_flag()) {
			userBt = (Button) findViewById(R.id.userExit);
			userBt.setVisibility(View.VISIBLE);
			userBt.setOnClickListener(new user());
			updateTime();
			// showInfo();
		}
		exitBt = (Button) findViewById(R.id.exit);
		exitBt.setOnClickListener(new exit());

		rbt = (Button) findViewById(R.id.btRefresh);
		rbt.setOnClickListener(new refresh());

		cityBt = (Button) findViewById(R.id.cityBT);
		cityBt.setOnClickListener(new city());

		findViewById(R.id.bbs_view).setVisibility(View.GONE);

	}

	private final class refresh implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (pa.isLogin_flag()) {
				showInfo();
			}
		}
	}

	private void updateTime() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (pa.isLogin_flag()) {
					handler_.sendMessage(handler_.obtainMessage(1));
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private final class user implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			pa.setLogin_flag(false);
			pa.setStu(null);
			Intent intent = new Intent();
			intent.setClass(MoreActivity.this, MainActivity.class);
			startActivity(intent);
		}
	}

	private final class exit implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showExitAlert();
		}

	}

	private void showExitAlert() {
		Builder builder = new Builder(MoreActivity.this);
		builder.setIcon(R.drawable.dialog);
		builder.setTitle(R.string.dailog);
		builder.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						SysApplication.getInstance().exit();
					}
				});
		builder.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.activity_dialog,
				(ViewGroup) findViewById(R.id.myLayout));
		builder.setView(layout);
		builder.show();
	}

	private void showInfo() {
		userTV = (TextView) findViewById(R.id.user_info);
		StringBuffer sb = new StringBuffer();
		sb.append(pa.getStu().getStu_nick()).append(",欢迎登录BBS在线论坛\n")
				.append("当前时间:").append(PublicUtil.current());
		userTV.setText(sb.toString());
	}

	private final class city implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			cityCt = (EditText) findViewById(R.id.cityET);
			String cityname = cityCt.getText().toString();
			if (cityname.equals("") || cityname == null) {
				cityCt.setError("请输入城市名称!");
				return;
			}
			pa.setCityName(cityname.trim());
			Toast.makeText(MoreActivity.this, "设置成功!", Toast.LENGTH_SHORT)
					.show();
			cityCt.setText("");
		}
	}

	@Override
	public void postLists() {
		// TODO Auto-generated method stub
	}

}
